home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.10 Oct 88 / XCMD < prev   
Encoding:
Text File  |  1988-09-27  |  6.4 KB  |  360 lines  |  [TEXT/elb ]

  1.  
  2.  
  3. {********************************}
  4. {* File: Reporter.p                *}
  5. {*                                        *}
  6. {* Prints the entire                 *}
  7. {* contents of the container        *}
  8. {*                                     *}
  9. {* ------------------------        *}
  10. {* In:    params[1] = handle         *}
  11. {* to the text to be printed        *}
  12. {*                                        *}
  13. {* ------------------------        *}
  14. {* © 1988, Donald Koscheka,        *}
  15. {*    All Rights Reserved                *}
  16. {* ------------------------        *}
  17. {********************************}
  18.  
  19. (****************************
  20.         BUILD SEQUENCE
  21.  
  22. pascal Reporter.p
  23. link -m ENTRYPOINT -rt ∂
  24. XCMD=6555 -sn Main=Reporter ∂
  25. Reporter.p.o ∂
  26. "{Libraries}"Interface.o ∂
  27. "{PLibraries}"Paslib.o ∂
  28. -o "{xcmds}"testxcmds
  29.  
  30. *****************************)
  31.  
  32. {$S Reporter }
  33.  
  34. UNIT Donald_Koscheka; 
  35.  
  36. {----INTERFACE-----}
  37.  
  38. INTERFACE
  39.  
  40. USES
  41.     MemTypes, QuickDraw, OSIntf,
  42.     ToolIntf, PackIntf, HyperXCmd,
  43.     PrintTraps;
  44.  
  45.  
  46. PROCEDURE EntryPoint(pPtr:XCmdPtr);
  47.  
  48. {-----IMPLEMENTATION-----}
  49.  
  50. IMPLEMENTATION
  51.  
  52. {$R-}                    
  53. CONST
  54.     CARD        = TRUE;
  55.     BKGND        = FALSE;
  56.     NILCHAR    = $00;
  57.     CR            = $0D; 
  58.     TAB        = $09;
  59.     SPACE        = $20;
  60.     FF            = $0C; 
  61.     LINEFEED    = $0A; 
  62.     QUOTE        = $22; 
  63.     COMMA        = $2C; 
  64.     PERIOD    = $2E;
  65.     PAREN        = $28;
  66.     
  67. TYPE
  68.     Str31 = String[31];            
  69.  
  70. PROCEDURE Reporter(pPtr:XCmdPtr);FORWARD;
  71.  
  72. {----EntryPoint-----}
  73.  
  74. PROCEDURE EntryPoint(pPtr: XCmdPtr);
  75. BEGIN
  76.     Reporter(pPtr);
  77. END;
  78.  
  79.  
  80. {-----Reporter-----}
  81.  
  82. Function CalcNextWord(VAR wPtr:Ptr):INTEGER;
  83. (**********************
  84. * Given a pointer to a 
  85. * word, calculate the 
  86. * length of the next word
  87. * in the run.  The length
  88. * is determined by adding
  89. * the width  of each character
  90. * together until a word
  91. * break is hit.
  92. *
  93. * The difference between 
  94. * tpos and wpos is always
  95. * one word (including the 
  96. * sticky characters)
  97. *
  98. * IN: Pointer to text 
  99. *    wPtr == pointer to NEXT word in
  100. *    run (or NIL if No next word)
  101. *
  102. * OUT:     Width of the next 
  103. * word in the line
  104. ***********************)
  105. VAR
  106.     done    : BOOLEAN;
  107.     wLen    : INTEGER;
  108. BEGIN
  109.     done := FALSE;
  110.     wLen := 0;
  111.         
  112. WHILE NOT done DO
  113. BEGIN CASE wPtr^ OF
  114.  
  115. CR,FF,LINEFEED,NILCHAR:
  116.                     done := TRUE;
  117. TAB:
  118.     BEGIN
  119.     WLen:=wLen+CharWidth( chr(SPACE) );
  120.     done := TRUE;
  121.     END;
  122.                     
  123.     SPACE, QUOTE, COMMA, PERIOD:    
  124.     BEGIN
  125.     wlen:=wLen+CharWidth( chr(wPtr^));
  126.     done := TRUE;
  127.     wPtr:=Pointer( ORD(wPtr) +1);
  128.     END;
  129.                     
  130. OTHERWISE
  131.     BEGIN
  132.     WLen:=wLen+CharWidth( chr(wPtr^));
  133.     wPtr := Pointer( ORD(wPtr) + 1 );
  134.     END;
  135. END; {*** CASE wPtr^ OF ***}
  136. END; {*** WHILE NOT done ***}
  137.         
  138. IF wPtr^ = 0 THEN wPtr := NIL;
  139.     
  140.     CalcNextWord := wLen;
  141. END;
  142.  
  143.  
  144. Procedure PrintHandle( hand : Handle; printer : TPPrPort; Prec : THPrint );
  145. (**********************
  146. * Print the data passed 
  147. * as a handle and 
  148. * using the given printer
  149. * port.
  150. *
  151. * Prints the data into 
  152. * the current port and
  153. * handles word wrap and
  154. * page breaks.
  155. *
  156. **********************)
  157. VAR 
  158.     done        : BOOLEAN;
  159.     lineHite,
  160.     wordSize    : INTEGER;
  161.     pageHite, 
  162.     pageWidth    : INTEGER;
  163.     num        : INTEGER;
  164.     cPos        : Ptr;    
  165.     wPos        : Ptr;
  166.     tPos        : Ptr;
  167.     lineRect    : Rect;
  168.     fontInfo    : FontInfo;
  169.     
  170. PROCEDURE EjectPage;
  171. (****************************
  172. * Eject the current page and 
  173. * adjust the rectangle 
  174. * accordingly
  175. *
  176. ****************************)
  177. BEGIN
  178.     PrClosePage( printer );
  179.     PrOpenPage( printer, NIL );
  180.     
  181. {*** Opening a new page yields    ***}
  182. {*** a new grafport, reset it    ***}
  183.     TextFont( GENEVA );
  184.     TextSize( 9 );
  185.     GetFontInfo( FontInfo );
  186.     lineHite := fontInfo.ascent +
  187.             fontInfo.descent +
  188.             fontInfo.leading;
  189.  
  190. WITH Prec^^.prInfo.rpage DO
  191. BEGIN
  192. lineRect.Top     := top;
  193. lineRect.Bottom:=lineRect.top                         +lineHite;
  194. lineRect.Right := lineRect.left;
  195. Moveto(lineRect.left,
  196. lineRect.bottom );
  197. END;
  198. tpos := Pointer( ORD( tpos ) +  1 );
  199. END;
  200.  
  201.  
  202. PROCEDURE NewLine;
  203. (******************************
  204. * Move to a new position on the
  205. * page.
  206. ******************************)
  207. BEGIN
  208.     WITH lineRect DO
  209.         BEGIN
  210.             top     := top + lineHite;
  211.             bottom := bottom + lineHite;
  212.             MoveTo( left, bottom );
  213.             
  214.             IF bottom > pageHite THEN 
  215.                 EjectPage;
  216.                 
  217.     Right := Prec^^.prInfo.rpage.left;
  218.         END;
  219. END;
  220.  
  221. PROCEDURE DrawLine;
  222. (******************************
  223. * Draw the current line please
  224. ******************************)
  225. BEGIN
  226.     DrawText( cPos, 0, num );
  227.     lineRect.Right := lineRect.left + wordSize;
  228.     tpos := Pointer( ORD( tpos ) + 1 ); { debug }
  229.     num := INTEGER( ORD(tPos) - ORD(wPos) );
  230.     cPos := wPos;
  231.     wPos := tPos;
  232. END;
  233.  
  234.     
  235. {----- Reporter -----}
  236. BEGIN
  237.     TextFont( GENEVA );
  238.     TextSize( 9 );
  239.     GetFontInfo( FontInfo );
  240.     lineHite := fontInfo.ascent + 
  241.                 fontInfo.descent + 
  242.                 fontInfo.leading;
  243.  
  244. {*** get information about page ***}
  245.     WITH Prec^^.prInfo.rpage DO    
  246.         BEGIN
  247.             pageHite         := bottom - top;
  248.             pageWidth     := right-left;
  249.             lineRect.top     := top;
  250.             lineRect.bottom:= top +                                 lineHite;
  251.             lineRect.left     := left;
  252.             lineRect.right     := left;
  253.             MoveTo( lineRect.left,                         lineRect.bottom );
  254.         END;
  255.  
  256.     Hlock( hand );
  257.     cPos := hand^;
  258.     wPos := cPos;
  259.     num    := 0;
  260.     done := false;
  261.     
  262.     REPEAT
  263.         tPos := wPos;
  264.         
  265.         CASE tPos^ OF
  266.             LINEFEED, CR:
  267.                 BEGIN
  268.                     DrawLine;
  269.                     NewLine;
  270.                     wpos := tpos;
  271.                 END;
  272.                 
  273.             FF:
  274.                 BEGIN
  275.                     EjectPage;
  276.                     wpos := tpos;
  277.                     lineRect.right := lineRect.right + wordSize;
  278.                 END;
  279.                     
  280.             NILCHAR:
  281.                 BEGIN
  282.                     DrawLine;
  283.                     done := TRUE;
  284.                 END;
  285.             
  286.             OTHERWISE
  287.                 BEGIN
  288.                     wordSize := CalcNextWord( tPos );
  289.                     IF( wordSize + lineRect.right ) < pageWidth THEN
  290.                         BEGIN
  291.                             num := num + INTEGER( ORD(tPos) - ORD(wPos) );
  292.                             wPos := tPos;
  293.                         END
  294.                     ELSE
  295.                         BEGIN
  296.                             DrawLine;
  297.                             NewLine;
  298.                         END;
  299.     lineRect.right:=lineRect.right + wordSize;
  300.                 END;
  301.                 
  302.         END {** CASE ***};
  303.     UNTIL done;
  304.     
  305.     PRClosePage( printer );
  306.     HUnlock( hand );
  307. END;
  308.  
  309.  
  310.  
  311. PROCEDURE Reporter(pPtr: XCmdPtr);
  312. (*********************
  313. * Print the data that's 
  314. * passed in as a 
  315. * parameter.
  316. **********************)
  317. VAR
  318.     fieldPtr        : Ptr;
  319.     fieldName        : Str255;
  320.     fieldType        : BOOLEAN;
  321.     fieldData        : Handle;
  322.     prRecHandle    : THPrint;
  323.     prPort        : TPPrPort;
  324.     myStRec        : TPrStatus;
  325.     
  326. {$I XCmdGlue.inc }
  327.  
  328. BEGIN
  329. WITH pPtr^ DO
  330. IF (paramCount <> 0) AND
  331. (params[1] <> NIL) THEN 
  332. BEGIN
  333.         
  334. PROpen;
  335. prRecHandle := THPrint(NewHandle(                         SIZEOF(TPRINT) ));
  336. PrintDefault( prRecHandle );
  337.             
  338. IF PRJobDialog( prRecHandle ) THEN BEGIN
  339. prPort := PROpenDoc( prRecHandle, NIL, NIL );
  340. PrOpenPage( prPort, NIL );
  341.                 
  342. PrintHandle( params[1], prPort, prRecHandle );
  343.                 
  344. PrCloseDoc( prPort );
  345.                 
  346.                 IF(prRecHandle^^.prJob.bJDocLoop = bSpoolLoop) AND (PrError = noErr) THEN
  347.     PRPicFile( prRecHandle, NIL, NIL,                 NIL, myStRec );
  348.  
  349. END; {*** IF PRJobDialog ***}
  350.             
  351.     DisposHandle(Handle(prRecHandle) );
  352.     PrClose;
  353.             
  354. END; {*** paramCount <> 0 ***}
  355. pPtr^.returnValue := NIL;
  356. END; {*** PROCEDURE Reporter ***}
  357.  
  358. END.
  359.  
  360.